**************************************************************************************
** title: How attitudes towards independence reshaped Scottish elections			**
**																					**					
** authors:	Daoust & Gareau-Paquette              			                        **
**************************************************************************************

clear all

*cd "C:/path/to/directory"

use "SES_pooled_cleaned.dta", clear

*Scheme setting
set scheme plotplain

* Rename variables according to the specified pattern
rename votel_pooled vote_list_abst
rename votec_pooled vote_district_abst
rename pid_pooled pid
rename indy_pooled indy
rename agegroup_pooled age_group
rename gender_pooled female
rename univ_pooled edu
rename year_id elecid
rename income_pooled income_cat
rename identity5_pooled identity5


* Generate an alternative vote choice variable that excludes abstainers (2 = abstainers/DK)
gen vote_list_noabst=vote_list_abst
recode vote_list_noabst (1=1) (2=.) (3=2) (4=3) (5=4) 

gen vote_district_noabst=vote_district_abst
recode vote_district_noabst (1=1) (2=.) (3=2) (4=3) (5=4)


*Recode variables for analyses
recode indy (1=0) (2=1)

label define indy_labels 0 "Opposed" 1 "In Favour"
label values indy indy_labels

		
*Remove 2014 Scottish Referendum Study

drop if elecid==2014

***************************************
***				Figure 1			***
***************************************	

* Distribution of INDY

ciplot indy, by(elecid) title("Support for Independence", box bexpand) note(" ") ///
			xtitle("") ytitle("Proportion of respondents favourable to independence", size(small)) ///
			ylabel(0.2(.1).5) 

***************************************
***				Figure 2			***
***************************************		
	
*The regression, predicting vote_list_noabst and only SES as controls
mlogit vote_list_noabst i.indy##i.elecid i.age_group female i.edu 
*Marginal effects
margins, dydx(indy) at(elecid==(2021 1999 2003 2007 2011 2016)) predict(outcome(4))  

	*SNP
marginsplot, title("Scottish National Party", box bexpand) ///
	yline(0) ylabel(-0.6(0.2)0.8) ytitle("Average Marginal Effect", size(small)) xtitle(" ") ///
	plotopts(color(sandb)  msymbol(circle)) ciopts(lcolor(sandb))
	
	graph save "snp.gph", replace
	
	*For the Labour
margins, dydx(indy) at(elecid==(1999 2003 2007 2011  2016 2021)) predict(outcome(2))  

	marginsplot, title("Scottish Labour", box bexpand) ///
	yline(0) ylabel(-0.6(0.2)0.8) ytitle("") xtitle(" ") ///
	plotopts(color(red)  msymbol(circle)) ciopts(lcolor(red))
	
	graph save labour.gph, replace
		
	*For the Conservative
margins, dydx(indy) at(elecid==(1999 2003 2007 2011  2016 2021)) predict(outcome(1))  

	marginsplot, title("Scottish Conservative & Unionist Party", box bexpand) ///
	yline(0) ylabel(-0.6(0.2)0.8) ytitle("Average Marginal Effect", size(small)) xtitle(" ") ///
	plotopts(color(blue)  msymbol(circle)) ciopts(lcolor(blue))
	
	graph save conservative.gph, replace	
	
	*For the Libdems	
margins, dydx(indy) at(elecid==(1999 2003 2007 2011  2016 2021)) predict(outcome(3))  

	marginsplot, title("Scottish Liberal Democrats", box bexpand) ///
	yline(0) ylabel(-0.6(0.2)0.8) ytitle("") xtitle(" ") ///
	plotopts(color(gold)  msymbol(circle)) ciopts(lcolor(gold))			
				
	graph save libdems.gph, replace	
		
	*Combine
graph combine snp.gph labour.gph conservative.gph libdems.gph		
					
			
***************************************
***			 Online	Appendix		***
***************************************	
	
***************************************
***				Figure A1			***
***************************************			 

*Figure A1. Generated using R script 
			
			
***************************************
***				Figure A2			***
***************************************		
	
*Replicating figure 2 but changing the DV
mlogit vote_district_noabst i.indy##i.elecid i.age_group female i.edu

*Marginal effects
margins, dydx(indy) at(elecid==(1999 2003 2007 2011 2016 2021)) predict(outcome(4))  

	*SNP
  	marginsplot, title("Scottish National Party", box bexpand) ///
	yline(0) ylabel(-0.6(0.2)0.8) ytitle("Average Marginal Effect", size(small)) xtitle(" ") ///
	plotopts(color(sandb)  msymbol(circle)) ciopts(lcolor(sandb))
	
	graph save "snp.gph", replace
	
	*For the Labour
margins, dydx(indy) at(elecid==(1999 2003 2007 2011  2016 2021)) predict(outcome(2))  

	marginsplot, title("Scottish Labour", box bexpand) ///
	yline(0) ylabel(-0.6(0.2)0.8) ytitle("") xtitle(" ") ///
	plotopts(color(red)  msymbol(circle)) ciopts(lcolor(red))
	
	graph save labour.gph, replace
		
	*For the Conservative
margins, dydx(indy) at(elecid==(1999 2003 2007 2011  2016 2021)) predict(outcome(1))  

	marginsplot, title("Scottish Conservative & Unionist Party", box bexpand) ///
	yline(0) ylabel(-0.6(0.2)0.8) ytitle("Average Marginal Effect", size(small)) xtitle(" ") ///
	plotopts(color(blue)  msymbol(circle)) ciopts(lcolor(blue))
	
	graph save conservative.gph, replace	
	
	*For the LiDems	
margins, dydx(indy) at(elecid==(1999 2003 2007 2011  2016 2021)) predict(outcome(3))  

	marginsplot, title("Scottish Liberal Democrats", box bexpand) ///
	yline(0) ylabel(-0.6(0.2)0.8) ytitle("") xtitle(" ") ///
	plotopts(color(gold)  msymbol(circle)) ciopts(lcolor(gold))			
				
	graph save libdems.gph, replace	
		
	*Combine
	graph combine snp.gph labour.gph conservative.gph libdems.gph	
	
	
***************************************
***				Figure A3			***
***************************************		
	
*The regression + survey weights
mlogit vote_list_noabst i.indy##i.elecid i.age_group female i.edu [pweight=weights_pooled]

*Marginal effects
margins, dydx(indy) at(elecid==(1999 2003 2007 2011 2016 2021)) predict(outcome(4))  

	*SNP
marginsplot, title("Scottish National Party", box bexpand) ///
	yline(0) ylabel(-0.6(0.2)0.8) ytitle("Average Marginal Effect", size(small)) xtitle(" ") ///
	plotopts(color(sandb)  msymbol(circle)) ciopts(lcolor(sandb))
	
	graph save "snp.gph", replace
	
	*For the Labour
margins, dydx(indy) at(elecid==(1999 2003 2007 2011  2016 2021)) predict(outcome(2))  

	marginsplot, title("Scottish Labour", box bexpand) ///
	yline(0) ylabel(-0.6(0.2)0.8) ytitle("") xtitle(" ") ///
	plotopts(color(red)  msymbol(circle)) ciopts(lcolor(red))
	
	graph save labour.gph, replace
		
	*For the Conservative
margins, dydx(indy) at(elecid==(1999 2003 2007 2011  2016 2021)) predict(outcome(1))  

	marginsplot, title("Scottish Conservative & Unionist Party", box bexpand) ///
	yline(0) ylabel(-0.6(0.2)0.8) ytitle("Average Marginal Effect", size(small)) xtitle(" ") ///
	plotopts(color(blue)  msymbol(circle)) ciopts(lcolor(blue))
	
	graph save conservative.gph, replace	
	
	*For the LiDems	
margins, dydx(indy) at(elecid==(1999 2003 2007 2011  2016 2021)) predict(outcome(3))  

	marginsplot, title("Scottish Liberal Democrats", box bexpand) ///
	yline(0) ylabel(-0.6(0.2)0.8) ytitle("") xtitle(" ") ///
	plotopts(color(gold)  msymbol(circle)) ciopts(lcolor(gold))			
				
	graph save libdems.gph, replace	
		
	*Combine
	graph combine snp.gph labour.gph conservative.gph libdems.gph		
					

***************************************
***				Figure A4			***
***************************************	
	
*Robustness checks. including more controls : income and PID

mlogit vote_list_noabst i.indy##i.elecid i.age_group female i.edu ib1.income_cat i.pid
*Marginal effects
margins, dydx(indy) at(elecid==(1999 2003 2007 2011 2016 2021)) predict(outcome(4))  

	*SNP
  	marginsplot, title("Scottish National Party", box bexpand) ///
	yline(0) ylabel(-0.5(0.1).5) ytitle("Average Marginal Effect") xtitle(" ") ///
	plotopts(color(sandb)  msymbol(circle)) ciopts(lcolor(sandb))
	
	graph save "snp.gph", replace
	
	*For the Labour
margins, dydx(indy) at(elecid==(1999 2003 2007 2011  2016 2021)) predict(outcome(2))  

	marginsplot, title("Scottish Labour", box bexpand) ///
	yline(0) ylabel(-0.5(0.1)0.5) ytitle("") xtitle(" ") ///
	plotopts(color(red)  msymbol(circle)) ciopts(lcolor(red))
	
	graph save labour.gph, replace
		
	*For the Conservative
margins, dydx(indy) at(elecid==(1999 2003 2007 2011  2016 2021)) predict(outcome(1))  

	marginsplot, title("Scottish Conservative & Unionist Party", box bexpand) ///
	yline(0) ylabel(-0.5(0.1)0.5) ytitle("Average Marginal Effect") xtitle(" ") ///
	plotopts(color(blue)  msymbol(circle)) ciopts(lcolor(blue))
	
	graph save conservative.gph, replace	
	
	*For the LiDems	
margins, dydx(indy) at(elecid==(1999 2003 2007 2011  2016 2021)) predict(outcome(3))  

	marginsplot, title("Scottish Liberal Democrats", box bexpand) ///
	yline(0) ylabel(-0.5(0.1)0.5) ytitle("") xtitle(" ") ///
	plotopts(color(gold)  msymbol(circle)) ciopts(lcolor(gold))			
				
	graph save libdems.gph, replace	
		
	*Combine
	graph combine snp.gph labour.gph conservative.gph libdems.gph		
				
***************************************
***				Figure A5			***
***************************************		
	
*The regression, predicting vote_list_noabst and only SES as controls + identity
mlogit vote_list_noabst i.indy##i.elecid i.age_group female i.edu i.identity5

*Marginal effects
margins, dydx(indy) at(elecid==(2021 1999 2003 2007 2011 2016)) predict(outcome(4))  

	*SNP
  	marginsplot, title("Scottish National Party", box bexpand) ///
	yline(0) ylabel(-0.6(0.2)0.8) ytitle("Average Marginal Effect", size(small)) xtitle(" ") ///
	plotopts(color(sandb)  msymbol(circle)) ciopts(lcolor(sandb))
	
	graph save "snp.gph", replace
	
	*For the Labour
margins, dydx(indy) at(elecid==(1999 2003 2007 2011  2016 2021)) predict(outcome(2))  

	marginsplot, title("Scottish Labour", box bexpand) ///
	yline(0) ylabel(-0.6(0.2)0.8) ytitle("") xtitle(" ") ///
	plotopts(color(red)  msymbol(circle)) ciopts(lcolor(red))
	
	graph save labour.gph, replace
		
	*For the Conservative
margins, dydx(indy) at(elecid==(1999 2003 2007 2011  2016 2021)) predict(outcome(1))  

	marginsplot, title("Scottish Conservative & Unionist Party", box bexpand) ///
	yline(0) ylabel(-0.6(0.2)0.8) ytitle("Average Marginal Effect", size(small)) xtitle(" ") ///
	plotopts(color(blue)  msymbol(circle)) ciopts(lcolor(blue))
	
	graph save conservative.gph, replace	
	
	*For the LiDems	
margins, dydx(indy) at(elecid==(1999 2003 2007 2011  2016 2021)) predict(outcome(3))  

	marginsplot, title("Scottish Liberal Democrats", box bexpand) ///
	yline(0) ylabel(-0.6(0.2)0.8) ytitle("") xtitle(" ") ///
	plotopts(color(gold)  msymbol(circle)) ciopts(lcolor(gold))			
				
	graph save libdems.gph, replace	
		
	*Combine
	graph combine snp.gph labour.gph conservative.gph libdems.gph						

	
	
			
*********************************************
***				Table  A3				  ***
*** (Tables A1 and A2 do NOT use data)    ***
*********************************************	

*Table A2 - descriptives for every variable
eststo clear

sort elecid	
by elecid: eststo: quietly estpost summarize indy ///
					age_group female edu income_cat pid 

esttab using "Descriptives.rtf", ///
		cells("min mean(fmt(3)) sd(fmt(3)) max count") ///	
		label mlabels(,titles) replace	
		

***************************************
***				Table A4     		***
***************************************	
 
*Appendix table
eststo m1:   mlogit vote_list_noabst i.indy##i.elecid i.age_group female i.edu

  esttab ///
        m1 using "ma4.rtf", ///
			label nogap  compress ///
			pr2  b(2) se(2) nonumbers replace  noomitted nobase unstack 		
					


	
	
***************************************
***				Table A5     		***
***************************************	
 
*Appendix table
 eststo m1:   mlogit vote_district_noabst i.indy##i.elecid i.age_group female i.edu 

  esttab ///
        m1 using "ma5.rtf", ///
			label nogap  compress ///
			pr2  b(2) se(2) nonumbers replace  noomitted nobase unstack 
			
			
***************************************
***				Table A6   		***
***************************************	
 
*Appendix table
eststo m1:   mlogit vote_list_noabst i.indy##i.elecid i.age_group female i.edu [pweight=weights_pooled]


  esttab ///
        m1 using "ma6.rtf", ///
			label nogap  compress ///
			pr2  b(2) se(2) nonumbers replace  noomitted nobase unstack 				

			
***************************************
***				Table A7     		***
***************************************	
 
*Appendix table
eststo m1:   mlogit vote_list_noabst i.indy##i.elecid i.age_group female i.edu ib1.income_cat i.pid

  esttab ///
        m1 using "ma7.rtf", ///
			label nogap  compress ///
			pr2  b(2) se(2) nonumbers replace  noomitted nobase unstack 	
			
			
			
***************************************
***				Table A8     		***
***************************************	
 
*Appendix table
eststo m1:   mlogit vote_list_noabst i.indy##i.elecid i.age_group female i.edu i.identity5

  esttab ///
        m1 using "ma8.rtf", ///
			label nogap  compress ///
			pr2  b(2) se(2) nonumbers replace  noomitted nobase unstack 		
					

		